<?php

namespace app\result;

use app\model\user\Cashflows;
use app\model\user\Categories;

class CategoryResult extends BaseResult
{

    public function list_option($user_id)
    {
        $model = new Categories();
        $data = $model->data_list_nopage(['user_id' => $user_id], 'id,name,type,icon', 'type desc,sort desc,id DESC');
        if ($data) {
            foreach ($data as &$v) {
                $v['type_name'] = $v['type'] == 10 ? '收入' : '支出';
                $v['value'] = $v['id'];
                $v['label'] = $v['name'] . '(' . $v['type_name'] . ')';
            }
        }
        return $data;
    }

    public function action($user_id, $args)
    {
        $type = intval($args['type'] ?? 0);
        $name = trim($args['name'] ?? '');
        $sort = intval($args['sort'] ?? 0);
        if (!$name) {
            return self::error_return('请输入分类名称');
        } elseif (mb_strlen($name) > 4) {
            return self::error_return('分类名称仅支持 4 个字');
        }
        if (!$type || !in_array($type, [10, 20])) {
            return self::error_return('请选择合适的分类类型');
        }
        $model = new Categories();
        $id = intval($args['id'] ?? 0);
        if ($id) {
            $data = $model->get_value(['id' => $id], 'id,name,type');
            if (!$data) {
                return self::error_return('分类不存在');
            }
        }

        $where = [
            'user_id' => $user_id,
            'name' => $name,
            'type' => $type,
        ];

        if ($has = $model->get_value($where, 'id')) {
            if (empty($id)) {
                return self::error_return('分类名称已存在');
            } elseif ($has != $id) {
                return self::error_return('分类名称已存在', $has);
            }
        }

        $baseData = [
            'user_id' => $user_id,
            'name' => $name,
            'type' => $type,
            'sort' => $sort,
            'icon' => !empty($args['icon']) ? $args['icon'] : 'red-packet'
        ];
        if ($id) {
            $model->data_update(['id' => $id], $baseData);
        } else {
            (new Categories())->data_add($baseData);
        }
        return self::success_return('保存成功');
    }

    //删除
    public function delete($user_id, $id)
    {
        if (!$id) {
            return self::error_return('请选择分类');
        }
        $model = new Categories();
        $data = $model->data_get(['id' => $id, 'user_id' => $user_id], 'id,name,type');
        if (!$data) {
            return self::error_return('分类不存在');
        }
        //有数据的不能删除
        if ((new Cashflows())->get_value(['user_id' => $user_id, 'category_id' => $id], 'id')) {
            return self::error_return('该分类下有数据，不能删除');
        }

        $res = $model->data_del(['id' => $id]);
        if (!$res) {
            return self::error_return('删除失败');
        }
        return self::success_return('删除成功');
    }

    public function get($user_id, $id)
    {
        $model = new Categories();
        $data = $model->data_get(['id' => $id, 'user_id' => $user_id], 'id,name,type,icon,sort');
        if (!$data) {
            return self::error_return('分类不存在');
        }
        return self::success_return('success', $data);
    }

    public function list($user_id, $args = [])
    {
        $type = intval($args['type'] ?? 0);//-1=管理分类的时候返回
        $where = [
            'user_id' => $user_id,
        ];
        if ($type && $type <> -1) {
            $where['type'] = $type;
        }

        $model = new Categories();
        $data = $model->data_list_nopage($where, 'id,name,type,icon', 'sort desc,id DESC');
        if ($type == -1) {
            return self::success_return('success', $data);
        }

        $accountTypes = ['微信', '支付宝', '银行卡', '现金', '信用卡', '其他'];
        foreach ($accountTypes as &$v) {
            $v = ['label' => $v, 'value' => $v];
        }
        $default_amount_type = (new Cashflows())->get_value(['user_id' => $user_id], 'amount_type', 'date desc');
        return self::success_return('success', [
            'category' => $data,
            'accountTypes' => $accountTypes,
            'default_amount_type' => $default_amount_type
        ]);
    }

    //初始化
    public static function init($user_id)
    {
        $ckey = 'category_init_abcef' . $user_id;
        if (cache($ckey)) return;
        $model = new Categories();
        if (!$model->get_value(['user_id' => $user_id], 'id')) {
            cache($ckey, 1, 10);
            $data = [
                ['name' => '工资', 'type' => 10, 'icon' => 'rmb-circle'],
                //['name' => '理财', 'type' => 10, 'icon' => 'red-packet'],
                //['name' => '兼职', 'type' => 10, 'icon' => 'red-packet'],
                //['name' => '人情', 'type' => 10, 'icon' => 'red-packet'],
                //['name' => '奖金', 'type' => 10, 'icon' => 'red-packet'],
                //['name' => '报销', 'type' => 10, 'icon' => 'red-packet'],
                //['name' => '生活费', 'type' => 10, 'icon' => 'red-packet'],
                ['name' => '其它', 'type' => 10, 'icon' => 'red-packet'],
                ['name' => '零花钱', 'type' => 10, 'icon' => 'rmb-circle'],
                ['name' => '交通', 'type' => 20, 'icon' => 'red-packet'],
                //['name' => '住房', 'type' => 20, 'icon' => 'red-packet'],
                ['name' => '餐饮', 'type' => 20, 'icon' => 'red-packet'],
                //['name' => '日用', 'type' => 20, 'icon' => 'red-packet'],
                //['name' => '人情', 'type' => 20, 'icon' => 'gift'],
                ['name' => '购物', 'type' => 20, 'icon' => 'shopping-cart'],
                //['name' => '医疗', 'type' => 20, 'icon' => 'red-packet'],
                //['name' => '教育', 'type' => 20, 'icon' => 'red-packet'],
                ['name' => '其它', 'type' => 20, 'icon' => 'red-packet'],
            ];
            foreach ($data as &$v) {
                $v['user_id'] = $user_id;
            }
            if (!$model->get_value(['user_id' => $user_id], 'id')) {
                (new Categories())->data_add($data, true);
            }
        }
    }

    public function icon_list()
    {
        $data = [
            '/static/image/icon/jianzhi.png',
            '/static/image/icon/chi.png',
            '/static/image/icon/meishi.png',
            '/static/image/icon/jiaoyu.png?v=1',
            '/static/image/icon/bao.png',
            '/static/image/icon/cart.png',
            '/static/image/icon/huazhuangpin.png',
            '/static/image/icon/jiushui.png',
            '/static/image/icon/trash.png',
            '/static/image/icon/yiliao.png?v=1',
            '/static/image/icon/hat.png',
            '/static/image/icon/meizhuang.png',
            '/static/image/icon/muying.png',
            '/static/image/icon/shoucang.png',
            '/static/image/icon/address.png',
            '/static/image/icon/sucai.png',
            '/static/image/icon/fruit.png',
            '/static/image/icon/xuezi.png',
            '/static/image/icon/yifu.png',
            '/static/image/icon/pay.png',
            '/static/image/icon/yan.png?v=1',
            'red-packet', 'zhifubao', 'order', 'chat', 'lock', 'lock-open', 'hourglass', 'home', 'star',
            'fingerprint', 'share', 'phone', 'gift', 'shopping-cart', 'grid', 'car', 'bag', 'rmb-circle',
            'thumb-up', 'kefu-ermai', 'camera', 'heart', 'integral',
        ];
        foreach ($data as &$v) {
            if (stripos($v, '/') !== false) {
                $v = request()->domain() . $v;
            }

            $v = ['icon' => $v, 'size' => 50];
        }
        unset($v);
        return self::success_return('success', [
            'list' => $data
        ]);
    }
}
